import FieldObject from "./FieldObject"
import GemDestroyAnimation from "./amn/GemDestroyAnimation"
import CellObject from "./cell"
import JumpText from "./jumptext"
import Utils from "./utils";
import GameConst from "./GameConst";

export default class ObjectPool{
    constructor() {
        this.fieldObjects = new Array;
        this.destroyAnimations = [];
        this.scores = {};
        var len = 90;
        for (var type = 0; type < 5; ++type) {
            if (type == 4){
                len = 15;
            }
            var n = [];
            for (var i = 0; i < len; ++i) {
                n.push(new FieldObject(type));
            }
            if (type < 4) {
                this.destroyAnimations.push([]);
                for (var i = 0; i < len; ++i){
                    this.destroyAnimations[type].push(new GemDestroyAnimation(type));
                }
            }
            this.fieldObjects.push(n);
        }
        for (var type = 3; type < 20; ++type) {
            var i = this.getText((type * CellObject.BASE_SCORE).toString(), null, 0, 0, 0);
            var s = this.getText((type * CellObject.BASE_SCORE).toString(), null, 0, 0, 0);
            this.returnText(i);
            this.returnText(s);
        }
    }


    getText(text, t, type, x, y) {
        if (typeof x === "undefined") {
            x = -1;
        }
        if (typeof y === "undefined") {
            y = -1;
        }
        var s = this.scores[text];
        if (!s) {
            s = [];
            this.scores[text] = s;
        }
        var textUI = s.length > 0 ? s.splice(0, 1)[0] : new JumpText(text, type);
        textUI.init(x > 0 || !t ? x : t.pos.x, y > 0 || !t ? y : t.pos.y, t);
        return textUI;
    };
}
    
    ObjectPool.prototype.returnText = function (e) {
        var t = e.text;
        var n = this.scores[t];
        if (!n) {
            n = [];
            this.scores[t] = n;
        }
        e.release();
        n.push(e);
    };
    ObjectPool.prototype.getObject = function (e, t, n, r, objType) {
        if (typeof objType === "undefined") {
            objType = -1;
        }
        if (objType > GameConst.level.assetNumber)
            objType = -1;
        while (true) {
            var s = objType >= 0 ? objType : Utils.getInt(Math.min(r, this.fieldObjects.length));
            if (this.fieldObjects[s].length > 0) {
                var o = this.fieldObjects[s].splice(0, 1)[0];
                return o.init(e, t, n);
            }
            objType = -1;
        }
        return null;
    };
    ObjectPool.prototype.returnObject = function (e) {
        e.release();
        this.fieldObjects[e.colorType].push(e);
    };
    ObjectPool.prototype.getDestroyAnimation = function (e, t, n, r) {
        var i = this.destroyAnimations[e].splice(0, 1)[0];
        i.init(t, n, r);
        return i;
    };
    ObjectPool.prototype.returnGemDestroy = function (anm) {
        this.destroyAnimations[anm.color].push(anm);
        anm.release();
    };

